package com.xuetang9.BBS.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.xuetang9.BBS.dao.ReplyDao;
import com.xuetang9.BBS.domain.Reply;
import com.xuetang9.BBS.domain.Topic;

@Repository
public class ReplyDaoImpl  implements ReplyDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	private RowMapper< Reply> getReplyRowMapper() {
		return new RowMapper< Reply>() {
			@Override
			public Reply mapRow(ResultSet resultSet, int arg1) throws SQLException {
				Reply reply= new Reply();
				reply.setAccount(resultSet.getString("account"));
				reply.setContent(resultSet.getString("content"));
				reply.setPrid(resultSet.getString("prid"));
				reply.setReplyDate(resultSet.getDate("replyDate"));
				reply.setRid(resultSet.getString("rid"));
				reply.setTid(resultSet.getString("tid"));
				return reply;	
			}
		};
	}
	
	public int save(Reply reply) {
		String sql = "insert into reply values( default,?,?,?,?,?)";
		return jdbcTemplate.update(sql,
				reply.getTid(),
				reply.getAccount(),
				reply.getReplyDate(),
				reply.getContent(),
				reply.getPrid()
				);
	}
	public List<Reply> selectTopicBytopic(Topic topic) {
		String sql1 = "update topic set visitCount = visitCount + 1 where tid = ?";
		String sql =  "select * from topic where tid=?";
		RowMapper<Reply> r = getReplyRowMapper();
		jdbcTemplate.update(sql1, r);
		return jdbcTemplate.query(sql,r);
	}	
}
